import { Component, OnInit } from '@angular/core';
import { Location } from '@angular/common';
import { UserPageProvider } from './pages/user/user.page.provider';
import { Router, NavigationEnd } from '@angular/router';


@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styles: [`
    .nav_bar_box{
      height: 56px;
      background:#3f51b5;
      position:fixed;
      left:0;top:0;
      width:100%;
      z-index:100;

    }
    
  `],
  providers: [UserPageProvider]

})
export class AppComponent {

  isLogin: boolean = false;
  showBackBtn: boolean = false;

  constructor(private location: Location, private userProvider: UserPageProvider, private router: Router) {


  }

  // 登录验证
  check() {
    let token = localStorage.getItem('token');
    if (token) {
      this.isLogin = true;
    } else {
      this.isLogin = false;
    }
  }

  ngOnInit() {
    this.router.events.subscribe((event) => {

      if (event instanceof NavigationEnd) {
        console.log(event);

        // 路由返回btn演示
        if (event.url == '/manhua') {
          this.showBackBtn = false;
        } else {
          this.showBackBtn = true;
        }

        // 登录
        if (!this.isLogin) {
          this.check();
        }

      }
    });
  }

  goBack() {
    this.location.back();
  }

}
